# -*- coding: utf-8 -*-
from db.mysql import MysqlClient
import requests
from common.helper import Helper
from common.config import *
from itertools import product
from urllib.parse import urlencode
name = "个人百万综合意外保险"
mysql = MysqlClient()
plan = {
    "planA": {
        "days": ["1m", "1y"],
        "people_count": ["1"],
        "accident_death": ["100000"],
        "airplane_death": ["300000"],
        "bus_death": ["100000"],
        "ship_death": ["100000"],
        "train_death": ["100000"],
        "accident_medical": ["10000"],
        "accident_hospital": ["60/天"]
    },
    "planB": {
        "days": ["1m", "1y"],
        "people_count": ["1"],
        "accident_death": ["300000"],
        "airplane_death": ["500000"],
        "bus_death": ["300000"],
        "ship_death": ["300000"],
        "train_death": ["300000"],
        "accident_medical": ["30000"],
        "accident_hospital": ["150/天"]
    },
    "planC": {
        "days": ["1m", "1y"],
        "people_count": ["1"],
        "accident_death": ["500000"],
        "airplane_death": ["800000"],
        "bus_death": ["500000"],
        "ship_death": ["500000"],
        "train_death": ["500000"],
        "accident_medical": ["50000"],
        "accident_hospital": ["250/天"]

    }

}
session = requests.session()
for k in plan:
    for v in product( plan[k]["days"],plan[k]["people_count"],plan[k]["accident_death"],plan[k]["airplane_death"],plan[k]["bus_death"],plan[k]["ship_death"],plan[k]["train_death"],plan[k]["accident_medical"],plan[k]["accident_hospital"]):

        data = Helper.formatProductInfo( plan[k],v )
        if data["days"] == "1m":
            endDate = "2020/06/01"
        else:
            endDate = "2021/05/01"
        if data["accident_death"] == "100000":
            items = "1524557557533"
        elif data["accident_death"] == "300000":
            items = "1524557557535"
        else:
            items = "1524557557536"
        payload = {
            "startdate": "2020 / 05 / 02",
            "enddate": endDate,
            "items": items,
            "peoplecount": "1",
            "planextendvalue":"",
            "productcode": "EAA_W"
        }


        s = session.post( url = domain[name]["url"],headers=domain[name]["header"],data=urlencode( payload).replace("+",""))
        if s.status_code == 200:
            price = s.json()[3]["originPremium"]
            res = {
                "name":name,
                "company":"中国人民保险",
                "price":Helper.decimalParse( price )
            }
            res.update( data  )
            Helper.mergeTable( name,mysql,res)


